package org.harlov.testsys.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.harlov.testsys.loger;
import org.harlov.testsys.db.suppdata.user_struct;

public class users {
	private static db_main DB;
	
	private static ArrayList<Object> students_view = new ArrayList<Object> (); //
	private static ArrayList<user_struct> students_list = new ArrayList<user_struct> ();
	private static ArrayList<Object> creators_list = new ArrayList<Object> ();
	
	public users (db_main DB_m) {
		DB = DB_m;
	}
		
	public static String proc_login (String _login , String _pass ) throws SQLException {
		String ans = "" ;	
		System.out.println(_login);
		System.out.println(DB.getMd5(_pass));
	
		ResultSet res = DB.makeReq("SELECT * FROM users WHERE login = '" + _login + "'");
		if (!res.next()) ans = "NF";
			else
		if (res.getBoolean("is_logined")) ans = "BL";
			else
		if (res.getString("pass").compareTo(DB.getMd5(_pass)) !=0) { ans = "EP" ; System.out.print(res.getString("pass"));}
			else {
					
					ans = "AC"+res.getInt("type") ;
					
					//int up_i = DB.makeUp("UPDATE users SET is_logined = TRUE WHERE login = '"+_login+"'");
					//System.out.print(up_i);
			}
		
		return ans;
		
	}
	
	public static int getId(String _login) {
		int ret = 0;
		try {
			ResultSet res =	DB.makeReq("SELECT id FROM users WHERE login='"+_login+"'");
			if (res.next()) {
				ret = res.getInt("id");
			}
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return ret;
	}
	
//СТУДЕНТЫ
	
	public static TableModel getUsersView(int type) throws SQLException {
		getUsers(type);
		studentsTableModel temp_model = new studentsTableModel(students_list);
		return temp_model;
	}
	
	
	
	public static ArrayList<user_struct> getUsers(int type) {
		students_list.clear();
		try {
			ResultSet res = DB.makeReq("SELECT * FROM users WHERE type='"+type+"'");
			while (res.next()) {
				user_struct temp = new user_struct(res.getInt("id"),res.getString("fio"),res.getString("login"),res.getString("pass"),res.getBoolean("is_logined"),res.getInt("type"));
				System.out.println(temp.getFio()+temp.getLogin()+temp.isLogined());
				students_list.add(temp);
			}
			
		}
		catch (SQLException e) {loger.add("getStudents",null, 1);}
		return students_list;
	}
	
	public static int getIdByUsersList(int ind) {
		return students_list.get(ind).getId();
	}
	
	
	
	public static int addUser(String _login,String _pass,String _fio,int user_type) {
		try {
			String [] prm = {_login,DB.getMd5(_pass),_fio,Integer.toString(user_type)};
			DB.makeUp("INSERT INTO users (login,pass,fio,type) VALUES ("+DB.makeParams(prm)+")");
			getUsers(user_type);
		}
		catch (SQLException e) { loger.add("addStudent", null, 1);}
		return 0;
		
	}
	
	
	public static int delUser(String _login,int type) {
	try {
		ResultSet res = DB.makeReq("SELECT id FROM users WHERE login ='"+_login+"'");
		if (res.next()) {
			int rem_id = res.getInt("id");
			DB.makeUp("DELETE FROM users WHERE id='"+Integer.toString(rem_id)+"'");
			getUsers(type);	
		}
	}
	catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return 0;
	}
	
	public static Vector<String> getTypes () {
		Vector<String> types = new Vector<String> () ;
		try {
			ResultSet res = DB.makeReq("SELECT name FROM type");
			while (res.next()) types.add(res.getString("name"));
		
		
		}
		catch (SQLException e) {loger.add(e.getMessage(), null, 1);}
		return types;
	}
	
	
	
public static class studentsTableModel extends AbstractTableModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String[] columnNames = {"ФИО","Логин","в системе"};
	private ArrayList<user_struct> data ; 
	
	public studentsTableModel(ArrayList<user_struct> m_data) {
		super();
		data = m_data;		
	}
	
	@Override
	public String getColumnName(int column) {
		return columnNames[column].toString();
		
	}
	
	@Override
	public int getColumnCount() {
		// TODO Auto-generated method stub
		 return columnNames.length;
	}

	@Override
	public int getRowCount() {
		// TODO Auto-generated method stub
		return data.size();
	}

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		user_struct tmp = data.get(rowIndex);
		Object ret = null;
		switch (columnIndex) {
			case 0: ret = tmp.getFio();break;
			case 1: ret = tmp.getLogin();break;
			case 2: ret = tmp.isLogined();break;
		}
		return ret;
	}
	
	public Class getColumnClass(int c) {
        return getValueAt(0, c).getClass();
    }
 
	
}

//РЕДАКТОРЫ
//АДМИНЫ
	
}
